//岗位
define(function() {

	var ViewModel = function() {
		var self = this;
		// 这里是分页配置,第一个参数是查询的地址，第二个参数是分页大小，第三个参数是回调函数
		self.DataGrid = new RIT.eW.DataGridAjax('/role/findAllRole', 20,
				self.jobs_process);
		self.jobs_process = function(role) {
			if (role && role.length && role.length > 0) {
				$.each(role, function(index, val) {
					val.checked = false;
				});
			}
			var items = ko.mapping.fromJS(role);
			return items();
		};

		// 搜索框
		self.filter = ko.observable({
			roleName : "",
			parentName : ""
		});

		// 查询
		self.search = function() {
			var data = self.filter();
			console.log(data);
			self.DataGrid.GridParams.pageIndex(1);
			self.DataGrid.GetData(data);

		};

		// 刷新
		self.search();
		// 重置
		self.empty = function() {
			self.filter({
				roleName : "",
				parentName : ""
			})
		};
		// 添加弹出框
		self.add = function() {
			var index = layer.open({
				type : 1,
				content : $('#pop_div_role')
			});
			self.model({
				roleName : '',
				parentName : '',
				note : ""
			})
		};
		// 关闭弹出框
		self.layerClose = function() {
			layer.close(layer.index);
			toastr.info("已关闭")
		};

		self.model = ko.observable({
			roleName : '',
			parentName : '',
			note : ""
		})

		// 保存
		self.submitbtn = function() {
			var data = self.model();
			console.log(data);
			$.ajax({
						url : "role/addOrUpdateRole",
						type : 'post',
						contentType : 'application/json',
						data : JSON.stringify(data),
						dataType : 'json',
						cache : false,
						success : function(result) {
							if (result.status == 0) {
								toastr.success(result.data, result.msg
										+ "保存成功后请刷新本页面");
								layer.close(layer.index);
								self.search();
							} else {
								toastr.error(result.msg);
							}
						}
					})
		}

		// 修改
		self.updateJobs = function(data) {
			layer.open({
				type : 1,
				content : $('#pop_div_role')
			});
			self.model(data);
		}
		// 删除
		self.deleteJobs = function(data) {
			// 询问框
			layer.confirm('你确定要删除吗？', {
				btn : [ '确定', '取消' ]
			// 按钮
			}, function() {
				$.ajax({
					url : "role/deleteRole",
					type : 'post',
					contentType : 'application/json',
					data : JSON.stringify(data),
					dataType : 'json',
					cache : false,
					success : function(result) {
						if (result.status == 0) {
							toastr.success(result.data, result.msg);
							self.search();
						} else {
							toastr.error(result.msg);
						}
					}
				})
				layer.msg('确定删除', {
					icon : 1
				});
			}, function() {
				layer.msg('已经取消', {
					time : 1000, // 1s后自动关闭
				});
			});
		}
		self.search();

		/** zTree树* */
		/** ztree */
		var ztree = "";
		self.ztreeshow = function() {
			var tree = {
				pFunction : {
					zTree : '',
					setting : {
						view : {
							dblClickExpand : false,
							selectedMulti : false,
							showTitle : false,
							showIcon : true
						},
						check : {
							enable : false
						},
						data : {
							key : {
								name : "roleName"
							},
							simpleData : {
								enable : true,
								idKey : "id",
								pIdKey : "pId",
								rootPId : null
							}
						},
						callback : {
							onClick : self.zTreeOnClick
						}
					}
				}
			};
			$.ajax({
				url : "/role/findRoleZtree",
				type : "post",
				dataType : "json",
				contentType : 'application/json;charset=UTF-8',
				success : function(result) {
					console.log(result);
					var zNodes = result;
					ztree = $.fn.zTree.init($("#treeDemo"),
							tree.pFunction.setting, zNodes);
					ztree.expandAll(true);
					console.log(ztree);
				}
			})
		};
		self.ztreeshow();
		self.zTreeOnClick = function(event, treeId, treeNode) {
			var node = ztree.getSelectedNodes();
			console.log(node[0].name, node[0].id);
			$("#treeDemo").css("display", "none")
		}
	}
	var model = new ViewModel();
	ko.applyBindings(model);
});